package edu.gatech.aristotle.random.inputGenerator;

import java.io.IOException;
import java.util.Random;

import net.util.File_Handler;

import edu.gatech.aristotle.random.util.CONSTANT_FILE_NAME;
import edu.gatech.aristotle.random.util.CONSTANT_SHCEDULE_INPUT_SPACE;
import edu.gatech.aristotle.random.util.RandomnessSelector;

public class Schedule_RandomInputGenerator implements CONSTANT_SHCEDULE_INPUT_SPACE, CONSTANT_FILE_NAME {
	public String doBatchInput() throws IOException{
		StringBuffer batchInput	= new StringBuffer();
		StringBuffer parameters	= new StringBuffer();
		
		for ( int i = 1; i < NUMBER_OF_TEST_CASE + 1; i++ ){	
			parameters.append( doRandomParameter() );
			
			if ( i < NUMBER_OF_TEST_CASE )	
				parameters.append( "\n" );
				
			String singleInput	= this.doInput();
		
			String testCaseFileName	= experimentDir +
										schedule + 
										testCaseNewComingDir + 
										i + 
										"_" + 
										SCHEDULE_TEST_FILE_NAME + 
										TEST_CASE_FILE_POSTFIX;
			File_Handler.write_to_file( testCaseFileName, singleInput );		
			
			batchInput.append( singleInput );
		}		

		String paraFileName	= experimentDir + schedule + testCaseNewComingDir + "parameters.txt";
		File_Handler.write_to_file( paraFileName, parameters.toString() );
		
		return batchInput.toString();
	}
	
	private String doRandomParameter() throws IOException {
		StringBuffer para = new StringBuffer();

		int para_1;
		int para_2;
		int para_3;
		
		if ( new Random().nextInt( 10 ) < 5 ){
			para_1	= new Random().nextInt( 3 ) + 1;
			para_2	= new Random().nextInt( 3 ) + 1;
			para_3	= new Random().nextInt( 3 ) + 1;
		}else{
			para_1	= new Random().nextInt( 10 ) - 1;
			para_2	= new Random().nextInt( 10 ) - 1;
			para_3	= new Random().nextInt( 10 ) - 1;
		}
		
		para.append( para_1 + " " );
		para.append( para_2 + " " );
		para.append( para_3 + " " );
		
		return para.toString();		
	}

	public String doInput(){
		StringBuffer input	= new StringBuffer();
		
		int randomInputLine	= new Random().nextInt( RANDOM_INPUT_LINE_SEED );
		
		for ( int i = 0; i < randomInputLine; i++ ){
			input.append( doOneLineInput() + "\n");
		}
		
		return input.toString();
	}
	
	public String doOneLineInput(){
		StringBuffer input	= new StringBuffer();
		
		int randomOperation	= new Random().nextInt( OPERATION_NUMBER );
		
		input.append( randomOperation );
		
		if ( randomOperation == 1 || randomOperation == 2){
			if ( new Random().nextInt( 10 ) < 8)
				input.append( " " + ( new Random().nextInt( 2 ) + 1 ) ); //reduce the scope for constrainted randomized inputs
			else 
				input.append( " " + new Random().nextInt( 20 ) );
		}
		
		if ( randomOperation == 2 || randomOperation == 4 ){
			input.append( " " + RandomnessSelector.randomFloat( FLOAT_SEED ) );		
		}
			
		/** illegal **/
		
		int randomChoice	= new Random().nextInt( OPERATION_NUMBER * 5 );
		
		if ( randomChoice == 0 ){
			input.append( " " + new Random().nextInt( OPERATION_NUMBER ) );
		}else if ( randomChoice == 1 ){
			input.append( " " + new Random().nextInt( OPERATION_NUMBER ) );
			
			int nextChoice	= new Random().nextInt( OPERATION_NUMBER );
			
			if ( nextChoice == 2 ){
				input.append( " " + RandomnessSelector.randomFloat( FLOAT_SEED ) );					
			}
		}else if ( randomChoice == 2 ){
			input.append( " " + RandomnessSelector.randomFloat( FLOAT_SEED ) );	
		}
		
		return input.toString();
	}
	
	public String doOneLineInputWithLooseConstraint(){
		StringBuffer input	= new StringBuffer();
		
		int randomOperation	= new Random().nextInt( OPERATION_NUMBER );
		
		input.append( randomOperation );
		
		if ( randomOperation == 1 ){
			if ( new Random().nextInt( 10 ) < 8)
				input.append( " " + ( new Random().nextInt( 6 ) - 1 ) );
			else 
				input.append( " " + new Random().nextInt( 20 ) );
		}
		
		if ( randomOperation == 2 ){

			input.append( " " + ( new Random().nextInt( 6 ) - 1 ) );
			input.append( " " + RandomnessSelector.randomFloat( FLOAT_SEED ) );		
		}
		
		if ( randomOperation == 4 ){
			input.append( " " + RandomnessSelector.randomFloat( FLOAT_SEED ) );		
		}
		
		/** illegal **/
		
		int randomChoice	= new Random().nextInt( OPERATION_NUMBER * 5 );
		
		if ( randomChoice == 0 ){
			input.append( " " + new Random().nextInt( OPERATION_NUMBER ) );
		}else if ( randomChoice == 1 ){
			input.append( " " + new Random().nextInt( OPERATION_NUMBER ) );
			
			int nextChoice	= new Random().nextInt( OPERATION_NUMBER );
			
			if ( nextChoice == 2 ){
				input.append( " " + RandomnessSelector.randomFloat( FLOAT_SEED ) );					
			}
		}else if ( randomChoice == 2 ){
			input.append( " " + RandomnessSelector.randomFloat( FLOAT_SEED ) );	
		}
		
		return input.toString();
	}
	
	
}
